using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_ZonesMenu : BaseUserControl
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            EnsureRoles(lnkHome, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Administrator });
            EnsureRoles(lnkSite, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer });
            EnsureRoles(lnkTemplates, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer });
            EnsureRoles(lnkMenus, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentViewer, Acms.Bll.Role.Roles.ContentPublisher });
            EnsureRoles(lnkTopics, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentViewer, Acms.Bll.Role.Roles.ContentPublisher });
            EnsureRoles(lnkPosts, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentViewer, Acms.Bll.Role.Roles.ContentPublisher });
            EnsureRoles(lnkUserGenerated, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.UserGeneratedContentEditor, Acms.Bll.Role.Roles.ContentPublisher });
            EnsureRoles(lnkUsers, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.MembershipViewer, Acms.Bll.Role.Roles.MembershipAdministrator });
            if (Page.AcmsContext.Site != null && Page.AcmsContext.Site.HasCustomAdminPages)
            {
                EnsureRoles(lnkCustom, new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Administrator });
            }
            else
            {
                lnkCustom.Visible = false;
            }

            if (Page is BaseUserPage)
            {
                SetSelected(lnkUsers);
            }
            else if (Page is BasePostPage)
            {
                SetSelected(lnkPosts);
            }
            else if (Page is BaseUserGeneratedContentPage)
            {
                SetSelected(lnkUserGenerated);
            }
            else if (Page is BaseTopicPage)
            {
                SetSelected(lnkTopics);
            }
            else if (Page is BaseMenuPage)
            {
                SetSelected(lnkMenus);
            }
            else if (Page is BaseSitePage)
            {
                SetSelected(lnkSite);
            }
            else if (Request.AppRelativeCurrentExecutionFilePath.ToLower().StartsWith("~/Custom/"))
            {
                SetSelected(lnkCustom);
            }
            else if (Request.AppRelativeCurrentExecutionFilePath.ToLower() == "~/default.aspx")
            {
                SetSelected(lnkHome);
            }
            else if (Request.AppRelativeCurrentExecutionFilePath.ToLower().StartsWith("~/templates/"))
            {
                SetSelected(lnkTemplates);
            }
        }

    }

    private void EnsureRoles(HyperLink lnk, Acms.Bll.Role.Roles[] roles)
    {
        lnk.Visible = false;
        foreach (Acms.Bll.Role.Roles role in roles)
        {
            if (Page.User == null)
            {
                if (role == Acms.Bll.Role.Roles.Anonomous)
                {
                    lnk.Visible = true;
                    return;
                }
            }
            else if (Page.User.HasRole(role))
            {
                lnk.Visible = true;
                return;
            }
        }
    }

    private void SetSelected(HyperLink link)
    {
        link.CssClass = string.Concat(link.CssClass, " selected");
        link.CssClass = link.CssClass.Trim();
    }

}
